package com.situ.mall.order.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.internal.util.AlipayUtils;
import com.situ.mall.base.config.AlipayConfigBean;
import com.situ.mall.base.entity.OrderInfo;
import com.situ.mall.base.service.IOrderInfoService;
import com.situ.mall.base.service.IPayService;
import com.situ.mall.base.util.C;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.order.entity.OrderPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: OrderController
 * Package: com.situ.mall.order.controller
 * Description:
 *
 * @Author Darling_达
 * @Create 2023-04-11 11:29
 * @Version 1.0
 */

@Controller
@RequestMapping(URLUtil.ORDER)
public class OrderController {

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private IPayService payService;

    @Autowired
    private AlipayConfigBean alipayConfig;


    @PostMapping(URLUtil.SAVE)
    public String save(String ids, Integer addId, Model model) {
        Map<String, Object> map = orderInfoService.saveOrder(ids, addId);
        // 生成订单
        model.addAttribute("qrCode", map.get("qrCode"));
        model.addAttribute("order",map.get("order"));
        return URLUtil.PAYMENT_HTML;
    }


    @PostMapping(URLUtil.NOTIFY)
    @ResponseBody
    public String notify(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name: requestParams.keySet()){
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i ++){
                valueStr = (i == values.length - 1) ? valueStr + values[i]: valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        // 验证信息签名是否正确
        boolean signVerified = false;
        try {
            signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipayPublicKey(), "UTF-8", alipayConfig.getSignType());
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        if (signVerified){
            if (params.get("trade_status").equals("TRADE_SUCCESS")){
                // 记录回调信息
                payService.payResult(params);
                // 修改本订单的状态
                orderInfoService.UpdateStatus(params.get("out_trade_no"), 1);
            }
            return C.SUCCESS;
        }
        return C.FAIL;
    }

    @GetMapping(URLUtil.PAYSTATUS + URLUtil.UID)
    @ResponseBody
    public Integer payStatus(@PathVariable Integer id){
        OrderInfo orderInfo = orderInfoService.getById(id);
        return orderInfo.getStatus();
    }


    // 监听队列  接收消息
    @JmsListener(destination = "order.save")
    public void orderCheck(Integer id){
        System.out.println("订单ID是：" + id);
    }



}
